//
//  WBNewFeature.swift
//  仿写微博
//
//  Created by Apple on 17/7/22.
//  Copyright © 2017年 tuimao. All rights reserved.
//

import UIKit

class WBNewFeature: UIView {

    @IBOutlet weak var scrollerView: UIScrollView!
    
    @IBOutlet weak var enterButton: UIButton!
    @IBOutlet weak var pageControl: UIPageControl!
    
    //进入微博
    @IBAction func enterStatus(_ sender: UIButton) {
        removeFromSuperview()
    }
    
    //xib加载视图
    class func WBNewFeature() -> WBNewFeature {
        
        let nib = UINib.init(nibName: "WBNewFeature", bundle: nil)
        
        let v = nib.instantiate(withOwner: nil, options: nil)[0] as! WBNewFeature
        
        v.frame = UIScreen.main.bounds
        
        return v
    }
    //MARK: 设置xib的视图
    override func awakeFromNib() {
       let count = 4
       //注意不能直接用bounds 这里的bounds是600*600
       let rect = UIScreen.main.bounds
        for i in 0..<count {
            //设置image
            let image = UIImage(named: "new_feature_\(i + 1)")
            //设置imageView
            let imageView = UIImageView.init(image: image)
            //设置imageView的大小
            imageView.frame = rect.offsetBy(dx:  CGFloat(i) * rect.width, dy: 0)
            
            scrollerView.addSubview(imageView)
        }
        //代理
        scrollerView.delegate = self
        //contentSize
        scrollerView.contentSize = CGSize(width: rect.width * (CGFloat(count) + 1), height: rect.height)
        //回弹
        scrollerView.bounces = false
        //整页滚动
        scrollerView.isPagingEnabled = true
        //水平垂直滚动条
        scrollerView.showsVerticalScrollIndicator = false
        scrollerView.showsHorizontalScrollIndicator = false
        //隐藏按钮
        enterButton.isHidden = true
        
    }
}
//MARK:UIScrollViewDelegate
extension WBNewFeature : UIScrollViewDelegate{
    //停止减速方法
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        //偏移量的整数
        let page = Int(scrollerView.contentOffset.x / scrollerView.bounds.width)
        //判断是否是最后一页
        if page == scrollerView.subviews.count{
            removeFromSuperview()
        }
        enterButton.isHidden = (page != scrollerView.subviews.count - 1)
    }
    //已经停止滚动
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        enterButton.isHidden = true
        let page = Int(scrollerView.contentOffset.x / scrollerView.bounds.width + 0.5)
        pageControl.currentPage = page
        //计算pageControl
        pageControl.isHidden = (page == scrollerView.subviews.count)
    }
    
}



